package com.cloud.prac.endecrypt;

import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * 摘要算法：单向、不可逆的；固定输出长度
 * - MD5
 * - SHA-1
 * - SHA-256
 * - SHA-512
 */
@Slf4j
public class DigestTest {

    // 加密算法
    private static final String ALGORITHM = "SHA-1";

    public static void main(String[] args) throws Exception {
        // e10adc3949ba59abbe56e057f20f883e
        String str = "123456";
        MessageDigest instance = MessageDigest.getInstance(ALGORITHM);
        byte[] bytes = instance.digest(str.getBytes(StandardCharsets.UTF_8));
        StringBuilder builder = new StringBuilder();
        for (byte aByte : bytes) {
            // 转成16进制；消息摘要使用16进制
            String hex = Integer.toHexString(aByte & 0xff);
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            builder.append(hex);
        }
        log.info(builder.toString());

    }
}
